<template>
  <div class="app-container">
    <!-- 创建 roll 房 -->
    <k-button v-if="authorityHandle(59)" class="top-btn" value="创建红包" @click.native="creatRedPocketDialog" />
    <!-- <k-button class="top-btn" value="玩法介绍" @click.native="ruleGuideDialog" /> -->

    <!--表格相关 -->
    <el-table
      :data="timeTableData"
      fit
      border
      highlight-current-row
      style="width: 100%;border-radius:8px;"
      :header-cell-style="{background:'#F6F5FA',color:'#4E4F62'}"
    >
      <el-table-column width="80px" type="index" label="序号" align="center" />
      <el-table-column prop="typeName" min-width="100px" label="红包类型" align="center" show-overflow-tooltip />
      <el-table-column prop="envelopeTheme" min-width="100px" label="红包主题" align="center" show-overflow-tooltip />
      <el-table-column min-width="100px" label="红包金额（F）" align="center" show-overflow-tooltip>
        <template slot-scope="scope">
          <span class="span_cloum" :class="{active: scope.row.giveType===1}">{{ scope.row.envelopeAmount }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="creatorName" min-width="100px" label="创建人" align="center" show-overflow-tooltip />
      <el-table-column min-width="100px" label="红包区间金额" align="center" show-overflow-tooltip>
        <template slot-scope="scope">
          <span class="span_cloum" :class="{active:scope.row.giveType===2}">{{ scope.row.minEnvelopeAmount }}-{{ scope.row.maxEnvelopeAmount }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="amount" min-width="100px" label="展示金额" align="center" show-overflow-tooltip />
      <el-table-column prop="envelopeNum" min-width="100px" label="红包个数" align="center" show-overflow-tooltip />
      <el-table-column min-width="100px" label="参与条件" align="center" show-overflow-tooltip>
        <template slot-scope="scope">
          <span>期间消费: {{ scope.row.rechargeTotal }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" min-width="110px" label="创建时间" align="center" />
      <el-table-column min-width="130px" label="开关" align="center">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.openStatus"
            active-color="#3b425d"
            inactive-color="#E9EBF2"
            active-text="开"
            inactive-text="关"
            :active-value="1"
            :inactive-value="2"
            :disabled="!authorityHandle(59)"
            @change="updateTimingStatus($event,scope)"
          />
        </template>
      </el-table-column>
      <el-table-column min-width="200px" label="操作" align="center">
        <template slot-scope="scope">
          <div class="op-div">
            <k-button class="table-btn" value="查看/修改" @click.native="updateRedDialog(scope.row)" />
            <k-button class="table-btn" value="详情" @click.native="shouRedBagDetail(scope.row.timingType)" />
            <k-button v-if="authorityHandle(59)" class="table-btn" value="删除" :disabled="scope.row.openStatus===1?true:false" @click.native="deleteRedItem(scope.row)" />
          </div>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="totalRows > 0"
      :total="totalRows"
      :page.sync="listQuery.pager.pageNo"
      :limit.sync="listQuery.pager.pageSize"
      @pagination="loadData"
    />
    <!-- 玩法介绍 弹窗 -->
    <k-dialog
      :show-hide.sync="ruleGuide.showHide"
      :modal-append-to-body="false"
      :append-to-body="true"
      title="玩法介绍"
      class-name="ruleGuide-dialog"
      :show-foot="false"
      :two-btn="authorityHandle(59)"
      @click="guideSave"
    >
      <div class="dialog-ruleGuide" style="padding:30px">
        <el-form :model="ruleGuideContent" label-width="auto">
          <el-form-item label="内容介绍：" prop="content">
            <quill-editor
              ref="myQuillEditor"
              v-model.trim="ruleGuideContent.content"
              :options="editorOption"
              class="editer"
            />
          </el-form-item>
        </el-form>
      </div>
    </k-dialog>
    <!-- 创建红包 弹窗 -->
    <k-dialog
      :show-hide.sync="creatRigisterRedPock.showHide"
      :modal-append-to-body="false"
      :append-to-body="true"
      :title="textMap[dialogStatus]"
      class-name="creatRigisterRedPock_dialog"
      :show-foot="false"
      :two-btn="authorityHandle(59)"
      @click="saveRegisterRedPock"
    >
      <div class="dialog_creatRigisterRedPock">
        <el-form :model="registerInfo" label-width="auto">
          <el-form-item label="红包类型：">
            <el-select :disabled="!authorityHandle(59)" v-model="registerInfo.timingType" placeholder="请选择类型">
              <el-option label="每日红包" :value="1" />
              <el-option label="每周红包" :value="2" />
              <el-option label="每月红包" :value="3" />
              <el-option label="每年红包" :value="4" />
            </el-select>
          </el-form-item>
          <el-form-item label="红包主题：">
            <el-input :readonly="!authorityHandle(59)" v-model="registerInfo.envelopeTheme" show-word-limit placeholder="请输入红包主题" />
          </el-form-item>
          <el-form-item label="红包简介：">
            <el-input
             :readonly="!authorityHandle(59)"
              v-model="registerInfo.envelopeIntroduce"
              type="textarea"
              :rows="3"
              show-word-limit
              placeholder="请输入红包简介"
            />
          </el-form-item>

          <el-form-item label="领取方式：">
            <div class="chargeform">
              <el-select :disabled="!authorityHandle(59)" v-model="registerInfo.receiveType" placeholder="请选择领取方式" class="red_money">
                <el-option label="进群领取" :value="1" />
                <el-option label="红包码领取" :value="2" />
              </el-select>
            </div>
          </el-form-item>
          <el-form-item label="展示金额：">
            <el-input :readonly="!authorityHandle(59)" v-model="registerInfo.amount" class="red_money" onkeyup="value=value.replace(/[^0-9.]/g,'')" placeholder="请输入数值" />
          </el-form-item>

          <el-form-item label="红包金额（F）：">
            <el-input :readonly="!authorityHandle(59)" v-model="registerInfo.envelopeAmount" class="red_money" :disabled="redMoneyFlag" onkeyup="value=value.replace(/[^0-9.]/g,'')" placeholder="请输入数值" />
            <el-radio-group :disabled="!authorityHandle(59)" v-model="giveType" class="radio_group" @change="radioChange">
              <el-radio :label="1" />
            </el-radio-group>
          </el-form-item>
          <el-form-item label="红包区间金额：">
            <el-input :readonly="!authorityHandle(59)" v-model="registerInfo.minEnvelopeAmount" class="red_money_small" :disabled="maxMinFlag" onkeyup="value=value.replace(/[^0-9.]/g,'')" placeholder="输入数值" />
            <i style="margin: 0 3px;"> — </i>
            <el-input :readonly="!authorityHandle(59)" v-model="registerInfo.maxEnvelopeAmount" class="red_money_small" :disabled="maxMinFlag" onkeyup="value=value.replace(/[^0-9.]/g,'')" placeholder="输入数值" />
            <el-radio-group :disabled="!authorityHandle(59)" v-model="giveType" class="radio_group" @change="radioChange">
              <el-radio :label="2" />
            </el-radio-group>
          </el-form-item>

          <el-form-item label="红包个数：">
            <el-input :readonly="!authorityHandle(59)" v-model="registerInfo.envelopeNum" class="red_money" onkeyup="value=value.replace(/[^0-9.]/g,'')" placeholder="请输入数值" />
          </el-form-item>
          <el-form-item label="期间消费(R)：">
            <el-input :readonly="!authorityHandle(59)" v-model="registerInfo.rechargeTotal" class="red_money" onkeyup="value=value.replace(/[^0-9.]/g,'')" placeholder="请输入数值" />
          </el-form-item>
        </el-form>
      </div>
    </k-dialog>
  </div>
</template>
<script>
import { listTimingData, saveTimingData, updateTimingData, deleteTimingData, updateTimingStatus, getDescribe, setDescribe } from '@/api/red/data'

import Pagination from '@/components/Pagination'

export default {
  components: { Pagination },
  data() {
    return {
      giveType: 1,
      redMoneyFlag: false,
      maxMinFlag: true,
      dialogStatus: 'create',
      textMap: {
        update: '修改红包',
        create: '创建红包'
      },
      totalRows: 0, // 分页组件--数据总条数
      listQuery: {
        pager: {
          pageNo: 1,
          pageSize: 10,
          totalRows: '',
          start: ''
        },
        sort: '', // 排序字段 为对应列名,
        direction: '', // 排序 正向(asc)逆向(desc),
        startTime: undefined, // 饰品外观',
        endTime: undefined, // 饰品类别',
        redPacketStatus: undefined, // 红包状态"',
        redPacketType: undefined, // 红包类型"',
        isModule: 1, // 是否模板 模板页传递1,其余传递2""',
        key: '' // 箱子名称或关键词 箱子配置界面所用'
      },
      value1: 1,
      timeTableData: [],
      ruleGuideContent: {
        content: ''
      },
      editorOption: { //  富文本编辑器配置
        placeholder: '请输入内容',
        modules: {
          toolbar: [
            ['bold', 'italic', 'underline', 'strike'], // 加粗，斜体，下划线，删除线
            ['blockquote', 'code-block'], // 引用，代码块
            [{ 'header': 1 }, { 'header': 2 }], // 标题，键值对的形式；1、2表示字体大小
            [{ 'list': 'ordered' }, { 'list': 'bullet' }], // 列表
            [{ 'script': 'sub' }, { 'script': 'super' }], // 上下标
            [{ 'indent': '-1' }, { 'indent': '+1' }], // 缩进
            [{ 'direction': 'rtl' }], // 文本方向
            [{ 'size': ['small', false, 'large', 'huge'] }], // 字体大小
            [{ 'header': [1, 2, 3, 4, 5, 6, false] }], // 几级标题
            [{ 'color': [] }, { 'background': [] }], // 字体颜色，字体背景颜色
            [{ 'font': [] }], // 字体
            [{ 'align': [] }], // 对齐方式
            ['clean'], // 清除字体样式
            ['image'] // 上传图片、上传视频
          ]
        }
      },
      registerInfo: {
        id: undefined,
        giveType: 1,
        parentId: undefined, // 父id，模板为-1",
        timingType: undefined, // 红包类型 天周月年",
        envelopeTheme: undefined, // 红包主题",
        envelopeIntroduce: undefined, // 红包介绍",
        receiveType: undefined, // 领取方式",
        envelopeAmount: undefined, // 红包金额（积分）",
        amount: undefined, // 展示金额",
        envelopeNum: undefined, // 红包个数",
        minEnvelopeAmount: undefined, // 红包个数",
        maxEnvelopeAmount: undefined, // 红包个数",
        rechargeTotal: undefined, // 期间消费",
        openStatus: undefined, // 开关",
        timeStatus: undefined, // 是否过期",
        startTime: undefined, // 红包起始时间",
        endTime: undefined, // 红包结束时间",
        envelopePassword: undefined, // 红包码",
        createTime: undefined // 创建时间"
      },
      timeValue: '',
      timeCotinue: '',
      conditions: {
        keyword: null
      },
      creatRigisterRedPock: {
        showHide: false
      },
      userListDialog: {
        showHide: false
      },
      ruleGuide: {
        showHide: false
      }
    }
  },
  watch: {
    // 时间赋值
    timeValue(val) {
      this.listQuery.startTime = this.timeValue ? this.timeValue[0] : null
      this.listQuery.endTime = this.timeValue ? this.timeValue[1] : null
    }
  },
  created() {
    this.loadData()
    this.getDescribe()
  },
  methods: {
    loadData() {
      listTimingData(this.listQuery).then(res => {
        this.totalRows = res.data.totalRows
        this.timeTableData = res.data.rows
      })
    },
    radioChange(v) {
      this.maxMinFlag = v === 1
      this.redMoneyFlag = v !== 1
    },
    // 保存玩法管理
    guideSave() {
      const contents = this.ruleGuideContent.content
      setDescribe({ 'key': 'envelopeTiming', 'data': contents }).then(res => {
        this.ruleGuide.showHide = false
      })
    },
    // 加载玩法管理
    getDescribe() {
      getDescribe({ key: 'envelopeTiming' }).then(res => {
        this.ruleGuideContent.content = res.data.msg
      })
    },
    creatRedPocketDialog() {
      this.$resetForm(this.registerInfo)
      this.dialogStatus = 'create'
      this.creatRigisterRedPock.showHide = true
    },
    updateRedDialog(row) {
      this.dialogStatus = 'update'
      this.creatRigisterRedPock.showHide = true
      this.registerInfo = row
      this.giveType = row.giveType
      this.radioChange(this.giveType)
    },
    // 保存红包
    saveRegisterRedPock() {
      this.registerInfo.giveType = this.giveType
      if (this.dialogStatus === 'create') {
        saveTimingData(this.registerInfo).then(res => {
          if (res.data.success) {
            this.$message.success('保存成功')
            this.$resetForm(this.registerInfo)
            this.loadData()
          } else {
            this.$message.success(res.data.msg)
          }
        })
      } else {
        updateTimingData(this.registerInfo).then(res => {
          if (res.data.success) {
            this.$message.success('修改成功')
            this.$resetForm(this.registerInfo)
            this.loadData()
          } else {
            this.$message.success(res.data.msg)
          }
        })
      }
      this.creatRigisterRedPock.showHide = false
    },
    // 状态
    updateTimingStatus(event, scope) {
      updateTimingStatus({ 'id': scope.row.id }).then(res => {
        if (res.data.success) {
          this.$message.success('操作成功')
        } else {
          this.$message.success(res.data.msg)
        }
      })
    },
    ruleGuideDialog() {
      this.ruleGuide.showHide = true
    },
    deleteRedItem(row) {
      this.$confirm('确定删除么?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteTimingData({ id: row.id }).then(res => {
          if (res.data.success) {
            this.$message.success('删除成功')
            this.loadData()
          } else {
            this.$message.success(res.data.msg)
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        })
      })
    },
    // 查看红包详请
    shouRedBagDetail(timingType) {
      this.$router.push({ path: '/redpockets/timepockets-detail', query: { timingType }})
    },
    // 搜索查询事件
    handleFilter() {
      this.listQuery.pager.pageNo = 1
      this.loadData()
    }

  }

}
</script>
<style lang="scss" scoped>
$menuText:#14162E; // 侧边栏字体颜色
$hoverColor:#1D143C;// 鼠标停留主色调浅色
$mainColor:#3b425d;// 主色调
::v-deep .el-radio__label{
  display: none;
}
.span_cloum{
  &.active{
    color: #3b425d;
  }
}
.filter-container {
  width: 100%;
  height: 48px;
  line-height: 48px;
  .filter-item {
    display: inline-block;
    vertical-align: middle;
    margin-bottom: 10px;
  }
  .kInput{
    position:absolute;
    right: 20px;
  }
  .filter-title{
    height: 48px;
    line-height: 48px;
    font-size: 16px;
    font-weight: 500;
    margin-right: 22px;
    color: #14162E;
  }
  .user-item {
    margin-left: 10px;
    margin-right: 20px;
  }
  .room-type,.user-status,.account-status{
    margin-right: 20px;
  }
}
.red_money{
  width: 200px;
}

.red_money_small{
  width: 100px;
}
.radio_group{
  margin-left: 30px;
}
.grid-div{
  display: grid;
  grid-template-columns:  repeat(7, 120px);
  .item{
    height: 50px;
    font-size: 14px;
    line-height:50px;
    text-align: center;
    border: 1px solid #E3E2E7;
    width: 120px;
    overflow: hidden;
    display: inline-block;
    text-overflow: ellipsis;
    white-space: nowrap;
    padding: 0 10px;
  }
}
.top-btn{
  margin: 30px 0px 30px 0px;
  font-size: 14px;
  color: #ffffff;
   &:nth-of-type(2){
    margin-left: 10px;
  }
}
.kSelect .el-select .el-input .el-input__inner{
  width: 124px;
}
::v-deep .el-select .el-input.is-focus .el-input__inner {
    border-color: $mainColor;
}
::v-deep .el-input__inner:focus {
    border-color: $mainColor;
}
.el-form-item__label-wrap .el-form-item__label{
  float: none;
}

.dialog_creatRigisterRedPock{
  display: flex;
  flex-direction: row;
  justify-content: center;
  margin: 40px 180px;
  ::v-deep .el-select .el-select__tags>span {
			display: none;
	}
  .el-tag + .el-tag {
    margin-left: 10px;
  }
   .el-tag {
    color: $mainColor;
    background-color: #ffffff;
    border-color: $mainColor;
    ::v-deep .el-tag__close {
      color: $mainColor;
    }
    ::v-deep .el-icon-close:hover {
      color:#ffffff;
      background: $mainColor;
    }
  }
}

.form-modify{
  display: flex;
  flex-direction: row;
  justify-content: center;
  margin: 10px 80px;
}

.editer {
  width: 500px;
  border-radius: 8px;
  float: left;
  box-sizing: border-box;
  color: #606266;
  ::v-deep .ql-container {
    height: 270px;
    border-radius: 0px 0px 8px 8px;
  }
  ::v-deep .ql-toolbar.ql-snow {
    border-radius: 8px 8px 0px 0px;
  }
}
.chargeform{
  display: flex;
  flex-direction: row;
  width: 300px;
}

::v-deep .el-switch__label.is-active {
    color: $mainColor;
}

.addBox_dialog{
  display: flex;
  flex-direction: column;
  justify-content: center;
  ::v-deep .el-dialog__body{
    width: 678px;
    margin: 40px 80px;
 }
::v-deep .el-select .el-select__tags>span {
			display: none;
	}
  .el-tag + .el-tag {
    margin-left: 10px;
  }
   .el-tag {
    color: $mainColor;
    background-color: #ffffff;
    border-color: $mainColor;
    ::v-deep .el-tag__close {
      color: $mainColor;
    }
    ::v-deep .el-icon-close:hover {
      color:#ffffff;
      background: $mainColor;
    }
  }
}
.cell-baby{
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: center;

}
.ellipsis-span{
  width: 80px;
  overflow: hidden;
  display: inline-block;
  text-overflow: ellipsis;
  white-space: nowrap;
}
</style>
